package com.eastcom.bm.dataprocess.processor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_ActionEmailNotice;
import com.eastcom.bm.atom.Atom_ActionRule;
import com.eastcom.bm.atom.Atom_CalendarItem;
import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_KpiDefine;
import com.eastcom.bm.atom.CalendarPolicy;
import com.eastcom.bm.dataprocess.processor.impl.RedirectProcessor;
import com.eastcom.bm.service.IActionRuleService;
import com.eastcom.bm.service.ICalendarItemService;
import com.eastcom.bm.service.ICalendarPolicyService;
import com.eastcom.bm.service.IDataSetService;
import com.eastcom.bm.service.IEmailNoticeService;
import com.eastcom.bm.service.IKpiDefineService;
import com.eastcom.bm.service.ServiceFactory;
import com.eastcom.canicula.stope.utils.MyLogger;
import com.eastcom.common.util.ObjectInfoManager;
import com.eastcom.messageserver.IMessagHandler;

public class CacheHandler implements IMessagHandler{

	public static String workdayHolidayPolicyUUID="001";
	
	public void handleObject(Object arg) throws Exception {
		if(arg instanceof CacheEditBean)
			executeUpdate((CacheEditBean)arg);
	}
	
	public void executeUpdate(CacheEditBean cacheEditBean){
		if(Atom_DatasetDefine.class==cacheEditBean.getBeanClass())
			editDataSetDefine(cacheEditBean.getBeanId(),cacheEditBean.getOperationType());
	}
	
	protected void editEmailNotice(String uuid,String operationType){
		Object synObj=ObjectInfoManager.lockOjbs.get(Atom_ActionEmailNotice.class);
		
		if("remove".equalsIgnoreCase(operationType)){
			synchronized(synObj){
				try{
				    ObjectInfoManager.removeObjectFromCache(Atom_ActionEmailNotice.class, uuid);
				    MyLogger.logger.info("remove EmailNotice with "+uuid+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove EmailNotice with "+uuid+" error", e);
				}
				synObj.notifyAll();
			}
			
			return;
		}
		
		IEmailNoticeService emailNoticeService=ServiceFactory.getInstance().getEmailNoticeService();
		Atom_ActionEmailNotice actionEmailNotice=emailNoticeService.findEmailNoticeByUUID(uuid);
		synchronized(synObj){
			try{
				ObjectInfoManager.removeObjectFromCache(Atom_ActionEmailNotice.class, uuid);
				ObjectInfoManager.addObjectIntoCache(actionEmailNotice, uuid);
			    MyLogger.logger.info(operationType+" EmailNotice with "+uuid+" completed");
			}
			catch(Exception e){
				MyLogger.logger.error("operationType+ EmailNotice with "+uuid+" error", e);
			}
			synObj.notifyAll();
		}
	}
	
	protected void editDataSetDefine(String dataSetId,String operationType){
		Object synObj=ObjectInfoManager.lockOjbs.get(Atom_DatasetDefine.class);
		
		if("remove".equalsIgnoreCase(operationType)){
			synchronized(synObj){
				try{
				    ObjectInfoManager.removeObjectFromCache(Atom_DatasetDefine.class, dataSetId);
				    MyLogger.logger.info("remove DataSetDefine with "+dataSetId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove DataSetDefine with "+dataSetId+" error", e);
				}
				synObj.notifyAll();
			}
			
			synObj=ObjectInfoManager.lockOjbs.get(Atom_KpiDefine.class);
			synchronized(synObj){
				try{
					Map valueMap=new HashMap();
					valueMap.put("datasetDefine/id", dataSetId);
				    ObjectInfoManager.removeObjectsFromCache(Atom_KpiDefine.class, "id",valueMap,true);
				    MyLogger.logger.info("remove KpiDefine with dataSetId "+dataSetId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove KpiDefine with dataSetId "+dataSetId+" error", e);
				}
				synObj.notifyAll();
			}
			
			synObj=ObjectInfoManager.lockOjbs.get(Atom_ActionRule.class);
			synchronized(synObj){
				try{
					Map valueMap=new HashMap();
					valueMap.put("datasetId", dataSetId);
				    //ObjectInfoManager.removeObjectsFromCache(Atom_ActionRule.class, "id",valueMap,true);
				    ObjectInfoManager.removeObjectFromCache(Atom_ActionRule.class, dataSetId);
				    MyLogger.logger.info("remove ActionRule with dataSetId "+dataSetId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove KpiDefine with dataSetId "+dataSetId+" error", e);
				}
				synObj.notifyAll();
			}
		}
		else{
		    IDataSetService dataSetService=ServiceFactory.getInstance().getDataSetService();
		    Atom_DatasetDefine dataSet=dataSetService.findDatasetDefineById(dataSetId);

		    if(dataSet==null)
		    	return;
		    
		    IKpiDefineService kpiService=ServiceFactory.getInstance().getKpiDefineService();
		    List<Atom_KpiDefine> kpiDefines=kpiService.selectKpiDefineByDataSetId(dataSetId);
		    
		    IActionRuleService actionRuleService=ServiceFactory.getInstance().getActionRuleService();
//		    Atom_ActionRule actionRule=actionRuleService.getActionRuleByDataSetId(dataSetId);
		    synchronized(synObj){
		    	try{
					ObjectInfoManager.addObjectIntoCache(dataSet, dataSetId);
//					ObjectInfoManager.addObjectIntoCache(actionRule, actionRule.getDatasetId());
					
					Map valueMap=new HashMap();
					valueMap.put("datasetDefine/id", dataSetId);
					ObjectInfoManager.removeObjectsFromCache(Atom_KpiDefine.class, "id",valueMap,true);
					ObjectInfoManager.addObjectsIntoCache(Atom_KpiDefine.class, kpiDefines, "id");
					
					MyLogger.logger.info(operationType+" DataSetDefine with "+dataSetId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error(operationType+" DataSetDefine with "+dataSetId+" error", e);
				}
				synObj.notifyAll();
		    }
		}
	}

	protected void editKpiDefineByDataSetId(String dataSetId,String operationType){
		IKpiDefineService kpiService=ServiceFactory.getInstance().getKpiDefineService();
	    List<Atom_KpiDefine> kpiDefines=kpiService.selectKpiDefineByDataSetId(dataSetId);
	    
	    Object synObj=ObjectInfoManager.lockOjbs.get(Atom_KpiDefine.class);
	    
	    synchronized(synObj){
	    	try{
	    		Map valueMap=new HashMap();
				valueMap.put("datasetDefine/id", dataSetId);
				ObjectInfoManager.removeObjectsFromCache(Atom_KpiDefine.class, "id",valueMap,true);
				ObjectInfoManager.addObjectsIntoCache(Atom_KpiDefine.class, kpiDefines, "id");
				
	    		MyLogger.logger.info(operationType+" kpiDefine with dataSetId "+dataSetId+" completed");
			}
			catch(Exception e){
				MyLogger.logger.error(operationType+" kpiDefine with dataSetId "+dataSetId+" error", e);
			}
			synObj.notifyAll();
	    }
	}
	
	protected void editCalandarPolicy(String policyId,String operationType){
		Object synObj=ObjectInfoManager.lockOjbs.get(CalendarPolicy.class);
		
		if("remove".equalsIgnoreCase(operationType)){
			synchronized(synObj){
				try{
				    ObjectInfoManager.removeObjectFromCache(CalendarPolicy.class, policyId);
				    if(workdayHolidayPolicyUUID.equals(policyId))
				    	RedirectProcessor.workHolidayCalendarPolicy=null;
				    
				    MyLogger.logger.info("remove CalendarPolicy with "+policyId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove CalendarPolicy with "+policyId+" error", e);
				}
				synObj.notifyAll();
			}
			
			synObj=ObjectInfoManager.lockOjbs.get(Atom_KpiDefine.class);
			synchronized(synObj){
				try{
					Map valueMap=new HashMap();
					valueMap.put("policyUUID", policyId);
				    ObjectInfoManager.removeObjectsFromCache(Atom_CalendarItem.class, "uuid",valueMap,false);
				    if(workdayHolidayPolicyUUID.equals(policyId))
				    	RedirectProcessor.specialWorkHolidayItems=null;
				    
				    MyLogger.logger.info("remove CalendarItem with CalendarPolicy "+policyId+" completed");
				}
				catch(Exception e){
					MyLogger.logger.error("remove CalendarItem with CalendarPolicy "+policyId+" error", e);
				}
				synObj.notifyAll();
			}
			
			return;
		}
		ICalendarPolicyService policyService=ServiceFactory.getInstance().getCalendarPolicyService();
		CalendarPolicy calendarPolicy=policyService.getCalendarPolicyByUUID(policyId);
		
		if(calendarPolicy==null)
			return;
		
		ICalendarItemService itemService=ServiceFactory.getInstance().getCalendarItemService();
		List<Atom_CalendarItem> items=itemService.selectCalendarItemByPolicyUUID(policyId);
		
		synchronized(synObj){
	    	try{
				ObjectInfoManager.addObjectIntoCache(calendarPolicy, policyId);
				Map valueMap=new HashMap();
				valueMap.put("policyUUID", policyId);
				ObjectInfoManager.removeObjectsFromCache(Atom_CalendarItem.class, "uuid",valueMap,false);
				ObjectInfoManager.addObjectsIntoCache(Atom_CalendarItem.class, items, "id");
				if(workdayHolidayPolicyUUID.equals(policyId)){
					RedirectProcessor.workHolidayCalendarPolicy=calendarPolicy;
					RedirectProcessor.specialWorkHolidayItems=items;
					MyLogger.logger.info(operationType+" RedirectProcessor workday holiday calendarPolicy with "+policyId+" completed");
				}
				
				MyLogger.logger.info(operationType+" calendarPolicy with "+policyId+" completed");
			}
			catch(Exception e){
				MyLogger.logger.error(operationType+" calendarPolicy with "+policyId+" error", e);
			}
			synObj.notifyAll();
	    }
	}
	
	public void addObject(String arg0, String arg1) {
	}

	public void deleteObject(String arg0, String arg1) {	
	}

	public void updateObject(String arg0, String arg1) {
	}
	
}
